{%extends "issue_base.html"%}

{%block head%}{{form.media}}{%endblock%}
{%block issue_body%}
{%if issue.draft_count or has_draft_message%}
  <div class="error">
    You have {%if issue.draft_count%}<b>{{issue.draft_count}} draft</b>
    comment{{issue.draft_count|pluralize}}{%endif%}
    {%if has_draft_message%}{%if issue.draft_count%}and {%endif%}a draft
    message{%endif%}.
    Drafts are not viewable by others;
    use <a class="novisit"
	   href="{%url codereview.views.publish issue.key.id%}">
    Publish+Mail Comments</a> ('m') to let others view them.
  </div>
{%endif%}


{%if issue.description%}
  <h3><a id="issue-description-pointer"
     href="javascript:M_toggleSection('issue-description')"
     class="toggled-section opentriangle">
    Description</a></h3>
  <div id="issue-description">{{issue.description|safe}}
  </div>
{%endif%}


{%for patchset in patchsets%}
  <h3>
    <a id="ps-{{patchset.key.id}}-pointer"
       href="{%url codereview.views.show issue.key.id%}#ps{{patchset.key.id}}"
       onclick="M_toggleSectionForPS('{{issue.key.id}}', '{{patchset.key.id}}')"
       class="toggled-section {%if forloop.last%}opentriangle{%endif%}">
      Patch Set {{forloop.counter}}
      {%if patchset.message%}: {{patchset.message}}{%endif%}
      <span class="anchor">#</span>
    </a>
    {%ifequal issue.owner user%}
      <a class="editpatchset" onclick="M_editPatchsetTitle(
          '{{issue.key.id}}', '{{patchset.key.id}}', '{{xsrf_token}}',
          {%if patchset.message%}'{{patchset.message|escapejs}}'{% else %}''{% endif %},
          '{{forloop.counter}}')">(edit)</a>
    {%endifequal%}
  </h3>

  {%if patchset.num_comments or patchset.n_drafts%}
    <div>
      <i>Total comments:</i> {{patchset.num_comments}}
      {%if patchset.n_drafts%}
        <span style="color:red">
          <b>+ {{patchset.n_drafts}} draft{{patchset.n_drafts|pluralize}}</b>
        </span>
      {%endif%}
    </div>
  {%endif%}

  <div id="ps-{{patchset.key.id}}"
  {%if forloop.last%}
       style="">
    {%include "patchset.html"%}
  {%else%}
       style="display:none">
  {%endif%}
  </div>
  {%if forloop.last%}
    <script language="JavaScript" type="text/javascript">
         <!--
         var lastPSId = {{patchset.key.id}};
         // -->
       </script>
  {%endif%}
{%endfor%}


{%if messages%}
  <h3>
    <a id="messages-pointer"
       href="javascript:M_toggleSection('messages')"
       class="toggled-section opentriangle">
      Messages
    </a>
  </h3>

  {%if messages%}
    <div><i>
    Total messages: {{messages|length}} ({{generated_messages|length}} generated)
    </i></div>
  {%endif%}

  <div id="messages">
    <div style="margin-bottom: .5em;">
      <a href="javascript:M_expandAllVisibleComments('cl', {{messages|length}})">
        Expand Messages</a>
      |
      <a href="javascript:M_collapseAllVisibleComments('cl', {{messages|length}})">
        Collapse Messages</a>
      {% if generated_messages %}
      |
      <a href="javascript:M_showGeneratedComments({{messages|length}})">
        Show Generated Messages</a>
      |
      <a href="javascript:M_hideGeneratedComments({{messages|length}})">
        Hide Generated Messages</a>
      {% endif %}
    </div>

    {%for message in messages%}
      <div class="message {%if message.issue_was_closed%}issue_was_closed{%endif%} {%if message.approval%}approval{%endif%} {%if message.disapproval%}disapproval{%endif%} {%if message.auto_generated %}generated{%endif%}"
           {%if message.auto_generated and messages|length != generated_messages|length and not display_generated_msgs %}style="display: none"{%endif%}
           id="{%if message.auto_generated %}generated-{%endif%}msg{{forloop.counter}}"
           name="{{forloop.counter0}}">
        <div class="header">
          <table border="0" width="100%" cellspacing="0" cellpadding="0">
            <tr class="comment_title"
                onclick="M_switchChangelistComment({{forloop.counter0}})">
              <td style="padding-left: 5px; white-space: nowrap;">
                <b>{%nickname message.sender%}</b>
              </td>
              <td width="100%" style="overflow:hidden;">
                <table style="table-layout:fixed; white-space: nowrap;"
                       width="100%">
                  <tr>
                    <td>
                      <span style="white-space: nowrap; overflow: hidden;{%if forloop.last or last_user_message_index == forloop.counter0%} display: none;{%endif%}"
                           class="extra"
                           id="cl-preview-{{forloop.counter0}}">
                          {{message.text|truncatewords:15}}
                      </span>
                    </td>
                  </tr>
                </table>
              </td>
              <td align="right"
                  style="white-space: nowrap; padding-right: 5px; padding-left: 3px;">
                {{message.date|timesince}} ago
		({{message.date|date:"Y-m-d H:i:s T"}})
                <a href="#msg{{forloop.counter}}">#{{forloop.counter}}</a>
              </td>
            </tr>
          </table>
        </div>

        <div id="cl-comment-{{forloop.counter0}}"
             {%if forloop.last or last_user_message_index == forloop.counter0%}{%else%}style="display: none;"{%endif%}>
          <div class="message-body">
            {%if message.issue_was_closed%}
              <span class="extra-note">
                Message was sent while issue was closed.
              </span>
            {%endif%}
            <pre name="cl-message-{{forloop.counter0}}"
            >{{message.text|wordwrap:80|urlizetrunc:80}}</pre>
          </div>
          <div class="message-actions">
            {%if user%}
              <a href="javascript:M_replyToMessage('{{forloop.counter0}}', '{{message.date|date:"Y/m/d H:i:s"}}', '{%nickname message.sender True%}', '{{message.key.id}}')"
                 id="message-reply-href-{{forloop.counter0}}">Reply</a>
              <textarea rows="20" name="message" style="display:none; width:99%"></textarea>
              <div id="message-reply-{{forloop.counter0}}"
                   style="display:none;"></div>
            {%else%}
              <a href="{{sign_in}}">Sign in</a> to reply to this message.
            {%endif%}
          </div>
        </div>

      </div>
    {%endfor%}

    <div>
      <a href="javascript:M_expandAllVisibleComments('cl', {{messages|length}})">
        Expand Messages</a>
      |
      <a href="javascript:M_collapseAllVisibleComments('cl', {{messages|length}})">
        Collapse Messages</a>
      {% if generated_messages %}
      |
      <a href="javascript:M_showGeneratedComments({{messages|length}})">
        Show Generated Messages</a>
      |
      <a href="javascript:M_hideGeneratedComments({{messages|length}})">
        Hide Generated Messages</a>
      {% endif %}
    </div>

  </div>

<script language="JavaScript" type="text/javascript">
<!--
var lastMsgID = {{messages|length}};
// -->
</script>

{%endif%}


<script language="JavaScript" type="text/javascript">
  <!--
    document.onkeydown = M_changelistKeyDown;
    document.onmousedown = M_changelistMouseDown;
    var dashboardState = new M_DashboardState(window, 'patch', 'M_CLPatchMarker');
    var issueId = {{issue.key.id}};
    M_toggleIssueOverviewByAnchor();
    M_showTryJobResult('tryjobdiv-{{last_patchset.key.id}}', false);
  // -->
</script>


{%if user%}
  <div style="display:none;">
    <form method="POST" action="{%url codereview.views.publish issue.key.id%}"
          id="message-reply-form">
      <input type="hidden" name="xsrf_token" value="{{xsrf_token}}">
      <div></div>
      <input type="hidden" name="in_reply_to" value="" />
      <input type="hidden" name="subject" value="{{issue.subject}}" />
      <input type="hidden" name="message_only" value="1" />
      <input type="submit" value="Send Message" />
      <input type="button" value="Discard" name="discard" />
      <input type="checkbox" name="send_mail" value="1"
        id="message-reply-send-mail" checked="checked" />
      <label for="message-reply-send-mail">Send mail to reviewers</label>
      <input type="hidden" name="add_as_reviewer" value="0" />
      <input type="checkbox" name="add_as_reviewer" value="1"
        id="message-reply-add-as-reviewer" />
        <label for="message-reply-add-as-reviewer">
          Add yourself as a reviewer</label>
    </form>
  </div>
  <a id="resizer" class="resizer" style="display:none;cursor:pointer">
    <img src="{{media_url}}zippyplus.gif">
  </a>

  <!-- Popup display to add and remove trybots from last patchset -->
  <div id="trybot-popup" style="display:none;">
    <form method="POST" action="" id="trybotform">
      <input type="hidden" name="xsrf_token" value="{{xsrf_token}}"/>
        <div id="trybot-popup-checkboxes">
        {% for tryserver, categories in default_builders.items|sort %}
          {% if categories.items|length == 1 and categories.keys.0 == "null" %}
            <div class="trybot-popup-builders">
            <b>{{ tryserver }}</b><br>
            {% for builder in categories.values.0|sort %}
                <input type="checkbox" name="{{tryserver}}:{{builder}}" id="cb_{{tryserver}}_{{builder}}">
                <label for="cb_{{tryserver}}_{{builder}}">{{builder}}</label><br>
            {%endfor%}
            </div>
          {% else %}
            <div class="trybot-popup-checkboxes-with-categories">
            <b>{{ tryserver }}</b><br>
            {% output_categories_and_builders tryserver categories.items %}
            </div>
          {% endif %}
          <div class="trybot-button-div">
            <input type="button" value="Update"
                   onclick="M_updatePendingTrybots('{{issue.key.id}}', '{{last_patchset.key.id}}', '{{xsrf_token}}')"/>
            <input type="button" value="Close"
                   onclick="M_closePendingTrybots()"/>
          </div>
        {% endfor %}
        </div>
    </form>
  </div>
{%endif%}

{%endblock issue_body%}
